//package com.info.zhiduoduo.core.service.payment.thirdpay;
//
//import com.alibaba.fastjson.JSON;
//import com.info.zhiduoduo.common.configs.CommonConfig;
//import com.info.zhiduoduo.common.configs.KltPayConfig;
//import com.info.zhiduoduo.common.configs.PayConfig;
//import com.info.zhiduoduo.common.configs.ZddUrlConfig;
//import com.info.zhiduoduo.common.constants.Constants;
//import com.info.zhiduoduo.common.constants.SmartResult;
//import com.info.zhiduoduo.common.constants.StatusMsg;
//import com.info.zhiduoduo.common.exception.RollbackException;
//import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
//import com.info.zhiduoduo.common.utils.LogUtil;
//import com.info.zhiduoduo.core.dto.project.PayResultDto;
//import com.info.zhiduoduo.core.service.WxMessageService;
//import com.info.zhiduoduo.core.service.ZhiduoduoProjectService;
//
//import com.info.zhiduoduo.repository.entity.order.TradeRecord;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.web.client.RestTemplate;
//
//import java.util.Date;
//
//import lombok.extern.slf4j.Slf4j;
//
//@Slf4j
//@Service
//public class KltPayService implements ThirdPayService {
//
//	@Autowired
//	ZhiduoduoProjectService zhiduoduoProjectService;
//	@Autowired
//	WxMessageService wxMessageService;
//
//	@Autowired
//	CommonConfig commonConfig;
//	@Autowired
//	ZddUrlConfig zddUrlConfig;
//	@Autowired
//	PayConfig payConfig;
//	@Autowired
//	KltPayConfig kltPayConfig;
//
//	@Override
//	public SmartResult doCallPayApi(TradeRecord tradeRecord) {
//		//调用第三方支付
//		RestTemplate rest = new RestTemplate();
//		BaseSimpleFormBean<TradeRecord> formBean = new BaseSimpleFormBean<>();
//		formBean.setReqdata(tradeRecord);
//
//		String url = zddUrlConfig.getBasePayUrl() + payConfig.getSinglePayUrl();
//		LogUtil.info("支付调用 url = " + url);
//		SmartResult smartResult = rest.postForObject(url, formBean, SmartResult.class);
//
//		// 测试
//		// PayResultDto payResultDto = new PayResultDto("1", "", "SUCCESS", "000000", "测试支付返回。");
//		// SmartResult smartResult = SmartResult.success(payResultDto);
//
//		return handleKltPayResult(tradeRecord, smartResult);
//	}
//
//	@Override
//	public SmartResult handlePayUserTradeState(TradeRecord trade, String orderState, String errorcode, String errormsg) {
//		if (trade == null) return SmartResult.success(StatusMsg.StatusMsg_444);
//
//		switch (orderState) {
//			case "CREATE":
//				trade.setTradestate(Constants.TRADE_CREATE);
//				break;
//			case "IN_PROCESS":
//				trade.setTradestate(Constants.TRADE_IN_PROCESS);
//				break;
//			case "SUCCESS":
//				trade.setTradestate(Constants.TRADE_SUCCESS).setFinishtime(new Date());
//				zhiduoduoProjectService.doAfterPayUserTradeSucceed(trade);
//				break;
//			case "FAIL":
//				trade.setTradestate(Constants.TRADE_FAIL).setFinishtime(new Date());
//				wxMessageService.notifyAdminPaymentFail(trade);
//				break;
//			case "REFUSE":
//				trade.setTradestate(Constants.TRADE_REFUSE).setFinishtime(new Date());
//				wxMessageService.notifyAdminPaymentFail(trade);
//				break;
//			default:
//				break;
//		}
//
//		trade.setErrorcode(errorcode).setErrormsg(errormsg);
//		if (trade.updateById()) return SmartResult.success();
//		return SmartResult.fail(StatusMsg.StatusMsg_445);
//	}
//
//	@Override
//	public SmartResult handlePayJiugeTradeState(TradeRecord trade, String orderState, String errorcode, String errormsg) {
//		if (trade == null) return SmartResult.success(StatusMsg.StatusMsg_444);
//
//		switch (orderState) {
//			case "CREATE":
//				trade.setTradestate(Constants.TRADE_CREATE);
//				break;
//			case "IN_PROCESS":
//				trade.setTradestate(Constants.TRADE_IN_PROCESS);
//				break;
//			case "SUCCESS":
//				trade.setTradestate(Constants.TRADE_SUCCESS).setFinishtime(new Date());
//				zhiduoduoProjectService.doAfterPayJiugeTradeSucceed(trade);
//				break;
//			case "FAIL":
//				trade.setTradestate(Constants.TRADE_FAIL).setFinishtime(new Date());
//				// wxMessageService.notifyAdminPaymentFail(trade);
//				break;
//			case "REFUSE":
//				trade.setTradestate(Constants.TRADE_REFUSE).setFinishtime(new Date());
//				// wxMessageService.notifyAdminPaymentFail(trade);
//				break;
//			default:
//				break;
//		}
//
//		trade.setErrorcode(errorcode).setErrormsg(errormsg);
//		if (trade.updateById()) return SmartResult.success();
//		return SmartResult.fail(StatusMsg.StatusMsg_445);
//	}
//
//	private SmartResult handleKltPayResult(TradeRecord trade, SmartResult smartResult) {
//		if (smartResult.getObject() == null || smartResult.isFailed()) {
//			//支付失败,回滚并显示失败原因
//			String rollbackMsg = "";
//			if (smartResult.getObject() != null) {
//				PayResultDto payResult = JSON.parseObject(JSON.toJSONString(smartResult.getObject()), PayResultDto.class);
//				rollbackMsg = payResult.getResponseMsg();
//			} else {
//				rollbackMsg = smartResult.getMsg();
//			}
//
//			LogUtil.info("支付失败,回滚并显示失败原因");
//			throw new RollbackException(rollbackMsg);
//
//		} else {
//			//支付接口调用成功,查看交易结果
//			PayResultDto payResult = JSON.parseObject(JSON.toJSONString(smartResult.getObject()), PayResultDto.class);
//			trade.setRequestid(payResult.getRequestId());
//
//			if (trade.getBusinessType() == Constants.KLT_PAY_BUSINESS_TYPE_USER_AMOUNT)
//				return handlePayUserTradeState(trade, payResult.getOrderState(), payResult.getResponseCode(), payResult.getResponseMsg());
//			else if (trade.getBusinessType() == Constants.KLT_PAY_BUSINESS_TYPE_JIUGE_CHARGE)
//				return handlePayJiugeTradeState(trade, payResult.getOrderState(), payResult.getResponseCode(), payResult.getResponseMsg());
//		}
//
//		return SmartResult.fail(StatusMsg.StatusMsg_445);
//	}
//}
